package com.ymatou.iapi.sellercs.testcase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellercs.parameter.PcLoginBean;
import com.ymatou.iapi.sellercs.service.PcLoginCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.SellerCSIMCallService;
import com.ymttest.database.model.YmtOnlinecs;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.database.sqlwapper.YmtSellerCSIMWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.GetLog;
import com.ymttest.utils.TestDataManager;
/**
*Pc登入
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_PcLogin{
	private static PcLoginBean pcloginBean;
	private static PcLoginCall pcloginCall;
	private static YmtSellerCSIMWapper YmtSellerCSIMWapper;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static int zsellerId = Integer.parseInt(EnvSetup.getData("autotest_seller_z"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("Pc登入");
	}

	@Before
	public void caseUp() throws ClientProtocolException, IOException {
		pcloginBean=new PcLoginBean();
		pcloginCall=new PcLoginCall();
		YmtSellerCSIMWapper=new YmtSellerCSIMWapper();
		SellerCSIMCallService.OffLine(sellerId, null);
		SellerCSIMCallService.OffLine(zsellerId, null);
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(pcloginCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_PcLogin_001() {
		Logger.start(true,"新数据Pc登入成功,通知买手服务10002-1,10001-1");
		try {
			Logger.comment("初始化数据");
			Integer csid=3383;
			Integer mcsid=3383;
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(csid);
			Logger.comment("记录日志");
			GetLog getLog=new GetLog("172.16.101.133", "sellercs.iapi.ymatou.com");
			String SocketIoToken=TestDataManager.getRandomUUID().toString().replaceAll("-", "");
			//post 表单不接受requestid
			getLog.startListenVm("default.log",".");
//			pcloginBean.setRequestId(SocketIoToken);
			pcloginBean.setMasterCsId(mcsid);
			pcloginBean.setCsId(csid);
			pcloginBean.setCsName("test");
			pcloginBean.setSocketIoToken(SocketIoToken);
			pcloginCall.setData(pcloginBean);
			pcloginCall.callService();		
			
			Logger.verifyIsContains("true", pcloginCall.getString("Result"), "验证IsSuccess");
			Logger.verifyIsContains("200", pcloginCall.getString("Status"), "验证Status");
			Thread.sleep(3000);
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
			//验证日志
			Logger.comment("验证日志包含发送指令10001");
			loglist.forEach(log->Logger.debug(log));
			List<String> filter = loglist.stream().filter(s->s.contains("10001")).collect(Collectors.toList());
			Logger.debug(filter);
			Logger.verifyEquals(true,filter.size()>=1,"验证日志");
			Logger.comment("验证日志包含发送指令10002");
			List<String> filter2 = loglist.stream().filter(s->s.contains("10002")).collect(Collectors.toList());
			Logger.debug(filter2);
			Logger.verifyEquals(true,filter2.size()>=1,"验证日志");
			
			YmtOnlinecs info = YmtSellerCSIMWapper.selectYmtOnlinecs(pcloginBean.getMasterCsId(), pcloginBean.getCsId()).get(0);
			Logger.debug(FormatUtil.beanToGSONString(info));
			Logger.verifyEquals(csid, info.getCSId(), "验证csid");
			Logger.verifyEquals(mcsid, info.getMasterCSId(), "验证mcsid");
			Logger.verifyEquals(pcloginBean.getCsName(), info.getCSName(), "验证csname");
			Logger.verifyEquals(1, info.getLoginStatus(), "验证loginStatus为Pc在线1");
			Logger.verifyEquals(0, info.getStatus(), "验证status为在线0");
			//时间允许误差
			Logger.verifyEquals(true,new Date().getTime()-info.getLastActiveTime().getTime()<10000,"验证最后时间");
			Logger.verifyEquals(SocketIoToken, info.getSocketIoToken(), "验证SocketIoToken");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_PcLogin_002() {
		Logger.start(true,"不输入MasterCsid失败");
		try {
			Logger.comment("初始化数据");
			Integer csid=3383;
			Integer mcsid=3383;
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(csid);
			Logger.comment("记录日志");
			String SocketIoToken=TestDataManager.getRandomUUID().toString().replaceAll("-", "");
			//post 表单不接受requestid
//			pcloginBean.setRequestId(SocketIoToken);
//			pcloginBean.setMasterCsId(mcsid);
			pcloginBean.setCsId(csid);
			pcloginBean.setCsName("test");
			pcloginBean.setSocketIoToken(SocketIoToken);
			pcloginCall.setData(pcloginBean);
			pcloginCall.callService();		
			
			Logger.verifyIsContains("400", pcloginCall.getString("Status"), "验证Status");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_PcLogin_003() {
		Logger.start(true,"不输入csname自动获取");
		try {
			Logger.comment("初始化数据");
			Integer csid=3383;
			Integer mcsid=3383;
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(csid);
			Logger.comment("记录日志");
			GetLog getLog=new GetLog("172.16.101.133", "sellercs.iapi.ymatou.com");
			String SocketIoToken=TestDataManager.getRandomUUID().toString().replaceAll("-", "");
			//post 表单不接受requestid
			getLog.startListenVm("default.log",".");
//			pcloginBean.setRequestId(SocketIoToken);
			pcloginBean.setMasterCsId(mcsid);
			pcloginBean.setCsId(csid);
//			pcloginBean.setCsName("test");
			pcloginBean.setSocketIoToken(SocketIoToken);
			pcloginCall.setData(pcloginBean);
			pcloginCall.callService();		
			
			Logger.verifyIsContains("true", pcloginCall.getString("Result"), "验证IsSuccess");
			Logger.verifyIsContains("200", pcloginCall.getString("Status"), "验证Status");
			Thread.sleep(3000);
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
			//验证日志
			Logger.comment("验证日志包含发送指令10001");
			loglist.forEach(log->Logger.debug(log));
			List<String> filter = loglist.stream().filter(s->s.contains("10001")).collect(Collectors.toList());
			Logger.debug(filter);
			Logger.verifyEquals(true,filter.size()>=1,"验证日志");
			Logger.comment("验证日志包含发送指令10002");
			List<String> filter2 = loglist.stream().filter(s->s.contains("10002")).collect(Collectors.toList());
			Logger.debug(filter2);
			Logger.verifyEquals(true,filter2.size()>=1,"验证日志");
			
			YmtOnlinecs info = YmtSellerCSIMWapper.selectYmtOnlinecs(pcloginBean.getMasterCsId(), pcloginBean.getCsId()).get(0);
			Logger.debug(FormatUtil.beanToGSONString(info));
			Logger.verifyEquals(csid, info.getCSId(), "验证csid");
			Logger.verifyEquals(mcsid, info.getMasterCSId(), "验证mcsid");
			Logger.verifyEquals(pcloginBean.getCsName()==null?new JUserWapper().selectUserinfoByUid(csid).get(0).getLoginId():pcloginBean.getCsName(), info.getCSName(), "验证csname");
			Logger.verifyEquals(1, info.getLoginStatus(), "验证loginStatus为Pc在线1");
			Logger.verifyEquals(0, info.getStatus(), "验证status为在线0");
			//时间允许误差
			Logger.verifyEquals(true,new Date().getTime()-info.getLastActiveTime().getTime()<10000,"验证最后时间");
			Logger.verifyEquals(SocketIoToken, info.getSocketIoToken(), "验证SocketIoToken");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_PcLogin_004() {
		Logger.start(true,"输入userid为子账号登入成功");
		try {
			Logger.comment("初始化数据");
			Integer csid=30245493;
			Integer mcsid=3383;
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(csid);
			Logger.comment("记录日志");
			GetLog getLog=new GetLog("172.16.101.133", "sellercs.iapi.ymatou.com");
			String SocketIoToken=TestDataManager.getRandomUUID().toString().replaceAll("-", "");
			//post 表单不接受requestid
			getLog.startListenVm("default.log",".");
//			pcloginBean.setRequestId(SocketIoToken);
			pcloginBean.setMasterCsId(mcsid);
			pcloginBean.setCsId(csid);
			pcloginBean.setCsName("test");
			pcloginBean.setSocketIoToken(SocketIoToken);
			pcloginCall.setData(pcloginBean);
			pcloginCall.callService();		
			
			Logger.verifyIsContains("true", pcloginCall.getString("Result"), "验证IsSuccess");
			Logger.verifyIsContains("200", pcloginCall.getString("Status"), "验证Status");
			Thread.sleep(3000);
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
			//验证日志 PCIMIsLogin	 opType=10002,op=1 
			Logger.comment("验证日志包含发送指令10002");
			List<String> filter2 = loglist.stream().filter(s->s.contains("opType=10002")).filter(s->s.contains("op=1")).collect(Collectors.toList());
			Logger.debug(filter2);
			Logger.verifyEquals(true,filter2.size()>=1,"验证日志");
			
			YmtOnlinecs info = YmtSellerCSIMWapper.selectYmtOnlinecs(pcloginBean.getMasterCsId(), pcloginBean.getCsId()).get(0);
			Logger.debug(FormatUtil.beanToGSONString(info));
			Logger.verifyEquals(csid, info.getCSId(), "验证csid");
			Logger.verifyEquals(mcsid, info.getMasterCSId(), "验证mcsid");
			Logger.verifyEquals(pcloginBean.getCsName(), info.getCSName(), "验证csname");
			Logger.verifyEquals(1, info.getLoginStatus(), "验证loginStatus为Pc在线1");
			Logger.verifyEquals(0, info.getStatus(), "验证status为在线0");
			//时间允许误差
			Logger.verifyEquals(true,new Date().getTime()-info.getLastActiveTime().getTime()<10000,"验证最后时间");
			Logger.verifyEquals(SocketIoToken, info.getSocketIoToken(), "验证SocketIoToken");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_PcLogin_005() {
		Logger.start(true,"输入userid不存在失败");
		try {
			Logger.comment("初始化数据");
			Integer csid=-3383;
			Integer mcsid=-3383;
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(csid);
			Logger.comment("记录日志");
			String SocketIoToken=TestDataManager.getRandomUUID().toString().replaceAll("-", "");
			//post 表单不接受requestid
//			pcloginBean.setRequestId(SocketIoToken);
			pcloginBean.setMasterCsId(mcsid);
			pcloginBean.setCsId(csid);
			pcloginBean.setCsName("test");
			pcloginBean.setSocketIoToken(SocketIoToken);
			pcloginCall.setData(pcloginBean);
			pcloginCall.callService();		
			
			Logger.verifyIsContains("400", pcloginCall.getString("Status"), "验证Status");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_PcLogin_006() {
		Logger.start(true,"App登入成功后Pc登入成功");
		try {
			Logger.comment("初始化数据");
			Integer csid=3383;
			Integer mcsid=3383;
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(csid);
			SellerCSIMCallService.AppLogin(csid);
			Logger.comment("记录日志");
			GetLog getLog=new GetLog("172.16.101.133", "sellercs.iapi.ymatou.com");
			String SocketIoToken=TestDataManager.getRandomUUID().toString().replaceAll("-", "");
			//post 表单不接受requestid
			getLog.startListenVm("default.log",".");
//			pcloginBean.setRequestId(SocketIoToken);
			pcloginBean.setMasterCsId(mcsid);
			pcloginBean.setCsId(csid);
//			pcloginBean.setCsName("test");
			pcloginBean.setSocketIoToken(SocketIoToken);
			pcloginCall.setData(pcloginBean);
			pcloginCall.callService();		
			
			Logger.verifyIsContains("true", pcloginCall.getString("Result"), "验证IsSuccess");
			Logger.verifyIsContains("200", pcloginCall.getString("Status"), "验证Status");
			Thread.sleep(3000);
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
			//验证日志
			Logger.comment("验证日志包含发送指令10001");
			loglist.forEach(log->Logger.debug(log));
			List<String> filter = loglist.stream().filter(s->s.contains("10001")).collect(Collectors.toList());
			Logger.debug(filter);
			Logger.verifyEquals(true,filter.size()>=1,"验证日志");
			Logger.comment("验证日志包含发送指令10002");
			List<String> filter2 = loglist.stream().filter(s->s.contains("10002")).collect(Collectors.toList());
			Logger.debug(filter2);
			Logger.verifyEquals(true,filter2.size()>=1,"验证日志");
			
			YmtOnlinecs info = YmtSellerCSIMWapper.selectYmtOnlinecs(pcloginBean.getMasterCsId(), pcloginBean.getCsId()).get(0);
			Logger.debug(FormatUtil.beanToGSONString(info));
			Logger.verifyEquals(csid, info.getCSId(), "验证csid");
			Logger.verifyEquals(mcsid, info.getMasterCSId(), "验证mcsid");
			Logger.verifyEquals(pcloginBean.getCsName()==null?new JUserWapper().selectUserinfoByUid(csid).get(0).getLoginId():pcloginBean.getCsName(), info.getCSName(), "验证csname");
			Logger.verifyEquals(3, info.getLoginStatus(), "验证loginStatus为都在线3");
			Logger.verifyEquals(0, info.getStatus(), "验证status为在线0");
			//时间允许误差
			Logger.verifyEquals(true,new Date().getTime()-info.getLastActiveTime().getTime()<10000,"验证最后时间");
			Logger.verifyEquals(SocketIoToken, info.getSocketIoToken(), "验证SocketIoToken");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_PcLogin_007() {
		Logger.start(true,"Pc重复登入成功,更新SocketIoToken,通知买手服务10002-1");
		try {
			Logger.comment("初始化数据");
			Integer csid=30245493;
			Integer mcsid=3383;
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(csid);
			Logger.comment("记录日志");
			GetLog getLog=new GetLog("172.16.101.133", "sellercs.iapi.ymatou.com");
			String SocketIoToken=TestDataManager.getRandomUUID().toString().replaceAll("-", "");
			//post 表单不接受requestid
			getLog.startListenVm("default.log",".");
//			pcloginBean.setRequestId(SocketIoToken);
			pcloginBean.setMasterCsId(mcsid);
			pcloginBean.setCsId(csid);
			pcloginBean.setCsName("test");
			pcloginBean.setSocketIoToken(SocketIoToken);
			pcloginCall.setData(pcloginBean);
			pcloginCall.callService();		
			
			Logger.verifyIsContains("true", pcloginCall.getString("Result"), "验证IsSuccess");
			Logger.verifyIsContains("200", pcloginCall.getString("Status"), "验证Status");
			Thread.sleep(3000);
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
			//验证日志
			Logger.comment("验证日志包含发送指令10001");
			loglist.forEach(log->Logger.debug(log));
			List<String> filter = loglist.stream().filter(s->s.contains("10001")).collect(Collectors.toList());
			Logger.debug(filter);
			Logger.verifyEquals(true,filter.size()>=1,"验证日志");
			Logger.comment("验证日志包含发送指令10002");
			List<String> filter2 = loglist.stream().filter(s->s.contains("10002")).collect(Collectors.toList());
			Logger.debug(filter2);
			Logger.verifyEquals(true,filter2.size()>=1,"验证日志");
			
			YmtOnlinecs info = YmtSellerCSIMWapper.selectYmtOnlinecs(pcloginBean.getMasterCsId(), pcloginBean.getCsId()).get(0);
			Logger.debug(FormatUtil.beanToGSONString(info));
			Logger.verifyEquals(csid, info.getCSId(), "验证csid");
			Logger.verifyEquals(mcsid, info.getMasterCSId(), "验证mcsid");
			Logger.verifyEquals(pcloginBean.getCsName(), info.getCSName(), "验证csname");
			Logger.verifyEquals(1, info.getLoginStatus(), "验证loginStatus为Pc在线1");
			Logger.verifyEquals(0, info.getStatus(), "验证status为在线0");
			//时间允许误差
			Logger.verifyEquals(true,new Date().getTime()-info.getLastActiveTime().getTime()<10000,"验证最后时间");
			Logger.verifyEquals(SocketIoToken, info.getSocketIoToken(), "验证SocketIoToken");		
			Logger.comment("修改SocketIoToken后再次调用");
			SocketIoToken=SocketIoToken+1;
			pcloginBean.setSocketIoToken(SocketIoToken);
			pcloginCall.callService();	
			Thread.sleep(1000);
			info = YmtSellerCSIMWapper.selectYmtOnlinecs(pcloginBean.getMasterCsId(), pcloginBean.getCsId()).get(0);
			Logger.verifyEquals(SocketIoToken, info.getSocketIoToken(), "验证SocketIoToken");
			//TODO 验一下2个浏览器登入同一账号是否有踢出机制
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_PcLogin_008() {
		Logger.start(true,"不输入Csid失败");
		try {
			Logger.comment("初始化数据");
			Integer csid=3383;
			Integer mcsid=3383;
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(csid);
			Logger.comment("记录日志");
			String SocketIoToken=TestDataManager.getRandomUUID().toString().replaceAll("-", "");
			//post 表单不接受requestid
//			pcloginBean.setRequestId(SocketIoToken);
			pcloginBean.setMasterCsId(mcsid);
//			pcloginBean.setCsId(csid);
			pcloginBean.setCsName("test");
			pcloginBean.setSocketIoToken(SocketIoToken);
			pcloginCall.setData(pcloginBean);
			pcloginCall.callService();		
			
			Logger.verifyIsContains("400", pcloginCall.getString("Status"), "验证Status");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_PcLogin_009() {
		Logger.start(true,"不输入SocketIoToken失败");
		try {
			Logger.comment("初始化数据");
			Integer csid=3383;
			Integer mcsid=3383;
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(csid);
			Logger.comment("记录日志");
			String SocketIoToken=TestDataManager.getRandomUUID().toString().replaceAll("-", "");
			//post 表单不接受requestid
//			pcloginBean.setRequestId(SocketIoToken);
			pcloginBean.setMasterCsId(mcsid);
			pcloginBean.setCsId(csid);
			pcloginBean.setCsName("test");
//			pcloginBean.setSocketIoToken(SocketIoToken);
			pcloginCall.setData(pcloginBean);
			pcloginCall.callService();		
			
			Logger.verifyIsContains("400", pcloginCall.getString("Status"), "验证Status");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}